package com.example.demo.service;

import com.example.demo.dao.NotShardingMapper;
import com.example.demo.dao.OrderMapper;
import com.example.demo.model.NotSharding;
import com.example.demo.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private NotShardingMapper notShardingMapper;
    public void add(Date createTime){
        Order order=new Order();
        order.setOrderId(1l);
        order.setUserId(2l);
        order.setCreateTime(createTime);
        orderMapper.insert(order);
    }

    public List<Order> select(Order order){
        return orderMapper.select(order);
    }

    public void addNotSharding(){
        NotSharding notSharding=new NotSharding();
        notSharding.setCreateTime(new Date());
        notShardingMapper.insert(notSharding);
    }

    public List<Order> selectByTime(String start, String end){
        return orderMapper.selectByTime(stringToDate(start),stringToDate(end));
    }

    public Date stringToDate(String string)  {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
